package pl.edu.agh.broker.portlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.broker.mns.client.MultiNameServiceLocator;
import pl.edu.agh.broker.mns.shared.MultiNameService;

public class ActivePortlet extends GenericPortlet {

	private static final String MNS_ADDRESS = "localhost";
	private static final int MNS_PORT = 50001;
	private static final String INDEXES_KEY = "all_indexes";

	private Map<String, String> indexesMap = new HashMap<String, String>();

	@SuppressWarnings("unchecked")
	public void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		MultiNameService mns = null;
		try {
			mns = MultiNameServiceLocator.getMultiNameService(MNS_ADDRESS,
					MNS_PORT);
			List<String> indexes = (List<String>) mns.getAny(INDEXES_KEY);
			for (String index : indexes) {
				String value = (String) mns.getAny(index);
				if (value != null) {
					indexesMap.put(index, value);
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		request.setAttribute("indexes", indexesMap);

		PortletRequestDispatcher prd = getPortletContext()
				.getRequestDispatcher("/jsp/stock.jsp");
		prd.include(request, response);

	}

}
